package com.fingard.dsp.bank.directConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.DSPSet;
import com.fingard.dsp.bank.reqrespobj.ReqRespHeader;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;

public class ReceiptProConfig extends BankConfig {

	public ReceiptProConfig(){
		init();
		fileSetList = new ArrayList<FileSaveSet>();
		pushSetList = new ArrayList<DataPushSet>();
	}
	
	/**回单文件下载保存主目录--本地*/
	public String localSaveMainDir;
	
	public boolean deleteAfterUploaded;
	
	/**回单数据推送分段笔数*/
	private String pushStepLen;
	
	/**转发回单文件上传和回单数据推送请求*/
	public boolean isTransmitReceiptReq = false;
	/**转发目的服务地址*/
	public String transmitDestServer;
	
	/**获取回单数据推送分段笔数*/
	public int getPushStepLen(){
		int retVal = 100;
		if(StringHelper.hasAnyChar(pushStepLen)){
			retVal = Integer.parseInt(pushStepLen);
		}
		if(retVal<=0){
			retVal = 10;
		}
		return retVal;
	}
	
	public ArrayList<DataPushSet> pushSetList;
	
	public ArrayList<FileSaveSet> fileSetList;
	public void loadConfig(String p_filePath) throws Exception{
		File tmpFile = new File(p_filePath);
        if (tmpFile.exists()) {
            String strXml = FileHelper.readToString(tmpFile, "utf-8");
            fromXmlStr(strXml);
            localSaveMainDir = DSPSet.replaceFilePath(getValueAsString("localSaveDir"));
            deleteAfterUploaded = getValueAsBool("deleteAfterUploaded");
            pushStepLen = getValueAsString("pushStepLen");
            isTransmitReceiptReq = this.getValueAsBool("isTransmitReceiptReq");
            transmitDestServer = getValueAsString("transmitDestServer");
            while(cfgXmlReader.read("/config/fileSaveSet")){
            	if(cfgXmlReader.readedXElement.getAttrValueAsBool("enable")){
	            	FileSaveSet tmpSet = new FileSaveSet();
	                tmpSet.fromXElement(cfgXmlReader.readedXElement);
	                fileSetList.add(tmpSet);
            	}
            }
            cfgXmlReader.backToTop();
            while(cfgXmlReader.read("/config/dataPushSet")){
            	if(cfgXmlReader.readedXElement.getAttrValueAsBool("enable")){
            		DataPushSet tmpSet = new DataPushSet();
	                tmpSet.fromXElement(cfgXmlReader.readedXElement);
	                pushSetList.add(tmpSet);
            	}
            }
        }
	}
	
	/**取得相对于localSaveMainDir的路径*/
	public String getPathRelativeToMain(String pPath){
		return StringHelper.getRelativePath(pPath, localSaveMainDir);
	}
	
	/**根据线路、主管组织、订户和日期，返回保存回单的相对目录，
	 * 返回如“201909\ICBC02_5000001”*/
	public String getRelativeSaveDir(ReqRespHeader pHeader, Date pBusDate){
		String busMonth = Format.Date6YearMonth.format(pBusDate);
		String subPath = StringHelper.joinNotEmpty("_", pHeader.bankCode, pHeader.corporation, pHeader.tenant);
		return busMonth + File.separator + subPath;
	}
	
	/**根据线路、主管组织、订户和日期，返回保存回单的目录，
	 * 返回如“...\receipt\201909\ICBC02_5000001”*/
	public String getSaveSubDir(ReqRespHeader pHeader, Date pBusDate){
		String saveDirPath = StringHelper.trimEndFileSp(localSaveMainDir) + File.separator + getRelativeSaveDir(pHeader, pBusDate);
		return saveDirPath;
	}
	
	/**根据相对目录，返回保存回单的目录，即返回localSaveMainDir拼接pRelative*/
	public String getSaveSubDirByRelative(String pRelative){
		return StringHelper.trimEndFileSp(localSaveMainDir) + File.separator + pRelative;
	}
}
